package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.EmpDepAddDTO;
import org.jsola.hr.dto.EmpDepBatchDTO;
import org.jsola.hr.dto.EmpDepUpdateDTO;
import org.jsola.hr.query.EmpDepQuery;
import org.jsola.hr.query.EmpInfoQuery;
import org.jsola.hr.service.IEmpDepService;
import org.jsola.hr.service.IEmpInfoService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


/**
 * @author zhr
 */
@RestController("hrEmpDepController")
@RequestMapping(HrConstants.API_PATH + "/v1/empDeps")
public class EmpDepController implements IEmpDepControllerAPI {

    @Autowired
    private IEmpDepService empDepService;

    @Autowired
    private IEmpInfoService empInfoService;

    @Override
    @PostMapping
    public Result<EmpDepVO> save(@Valid @RequestBody EmpDepAddDTO empDepAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增员工部门关系权限校验

        return Result.success(
                empDepService.save(empDepAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody EmpDepUpdateDTO empDepUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工部门关系权限校验

        int count = empDepService.update(empDepUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @PostMapping(value = "/batch")
    public Result batch(@Valid @RequestBody EmpDepBatchDTO empDepBatchDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量操作权限校验

        // 批量操作，注意校验，有越权风险
        // 新增
        List<EmpDepAddDTO> empDepAddDTOList = empDepBatchDTO.getEmpDepAddDTOList();
        if (!CollectionUtils.isEmpty(empDepAddDTOList)) {
            empDepService.batchSave(empDepAddDTOList,tokenUser);
        }
        // 修改
        List<EmpDepUpdateDTO> empDepUpdateDTOList = empDepBatchDTO.getEmpDepUpdateDTOList();
        if (!CollectionUtils.isEmpty(empDepUpdateDTOList)) {
            empDepUpdateDTOList.parallelStream().forEach(
                    empDepUpdateDTO -> {
                        empDepService.update(empDepUpdateDTO,tokenUser);
                    }
            );
        }
        // 删除
        List<String> deleteEmpDepIdList = empDepBatchDTO.getDeleteEmpDepIdList();
        if (!CollectionUtils.isEmpty(deleteEmpDepIdList)) {
            empDepService.deleteByIds(tokenUser,deleteEmpDepIdList.toArray(new String[0]));
        }
        return Result.success();
    }


    @Override
    @DeleteMapping(value = "/{empDepId}")
    public Result<Integer> delete(@PathVariable String empDepId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除员工部门关系权限校验

        int count = empDepService.deleteByIds(tokenUser, empDepId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] empDepIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除员工部门关系权限校验

        int count = empDepService.deleteByIds(tokenUser, empDepIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{empDepId}")
    public Result<EmpDepVO> findById(@PathVariable String empDepId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工部门关系权限校验

        return Result.success(
            empDepService.selectById(empDepId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<EmpDepListVO>> listAll(@Valid EmpDepQuery empDepQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看员工部门关系权限校验

        return Result.success(
            empDepService.select(empDepQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<EmpDepListVO>> page(@Valid EmpDepQuery empDepQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            empDepService.selectPage(empDepQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/getEmpListByDep")
    public Result<Page<EmpInfoListVO>> getEmpListByDep(@Valid EmpInfoQuery empInfoQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                empInfoService.pageOnJobEmpList(empInfoQuery, tokenUser)
        );
    }

    @Override
    @PutMapping(value = "/empDistribute")
    public Result<Integer> empDistribute(@Valid @RequestBody EmpDepAddDTO empDepAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改员工部门关系权限校验

        Integer count = empDepService.empDistribute(empDepAddDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping(value = "/deleteEmpByDep/{depId}")
    public Result<Integer> deleteEmpByDep(@PathVariable String depId,
                                          @RequestBody String[] empIds) {

        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                empDepService.deleteEmpByDep(depId,tokenUser,empIds)
        );
    }

    @Override
    @GetMapping(value = "/selectEmpByDepId")
    public Result<Page<EmpReduceInfoVO>> selectEmpByDepId(EmpDepQuery empDepQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        return Result.success(
                empDepService.selectEmpByDepId(empDepQuery, tokenUser)
        );
    }

}

